﻿namespace CachePage
{
    using System;
    using System.Web.Caching;
    using System.Web.UI;

    public class GuidCachePageStatePersister : PageStatePersister
    {
        private string _pageGuidKey;
        private TimeSpan limit;

        public GuidCachePageStatePersister(Page page, string pageGuidKey) : base(page)
        {
            this._pageGuidKey = pageGuidKey;
        }

        public GuidCachePageStatePersister(Page page, string pageGuidKey, TimeSpan cacheTimeOut) : base(page)
        {
            this._pageGuidKey = pageGuidKey;
            if (cacheTimeOut != TimeSpan.Zero)
            {
                this.limit = cacheTimeOut;
            }
            else
            {
                this.limit = new TimeSpan(0, 40, 0);
            }
        }

        protected virtual object GetCacheState(Page page, string key)
        {
            return page.Cache.Get(key);
        }

        private string GetViewStateGuidKey()
        {
            Guid guid;
            string str = base.Page.Request.Form["__VIEWSTATEGUID"];
            if (string.IsNullOrEmpty(str) || (str.Length < 1))
            {
                if ((this._pageGuidKey != null) && (this._pageGuidKey != ""))
                {
                    guid = new Guid(this._pageGuidKey);
                }
                else
                {
                    guid = Guid.NewGuid();
                }
                return guid.ToString();
            }
            try
            {
                Guid guid2 = new Guid(str);
                return guid2.ToString();
            }
            catch (FormatException)
            {
                guid = Guid.NewGuid();
                base.Page.ClientScript.RegisterHiddenField("__VIEWSTATEGUID", guid.ToString());
                return guid.ToString();
            }
        }

        protected virtual void InsertCacheState(Page page, string key, object value, TimeSpan limit)
        {
            page.Cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, limit);
        }

        public override void Load()
        {
            string viewStateGuidKey = this.GetViewStateGuidKey();
            base.ViewState = this.GetCacheState(base.Page, "ViewState_" + viewStateGuidKey);
            base.ControlState = this.GetCacheState(base.Page, "ControlState_" + viewStateGuidKey);
        }

        public override void Save()
        {
            if ((base.ViewState != null) || (base.ControlState != null))
            {
                if (base.Page.Cache == null)
                {
                    throw new InvalidOperationException("Cache needed for GuidCachePageStatePersister.");
                }
                string viewStateGuidKey = this.GetViewStateGuidKey();
                if (base.ViewState != null)
                {
                    this.InsertCacheState(base.Page, "ViewState_" + viewStateGuidKey, base.ViewState, this.limit);
                }
                if (base.ControlState != null)
                {
                    this.InsertCacheState(base.Page, "ControlState_" + viewStateGuidKey, base.ControlState, this.limit);
                }
                base.Page.ClientScript.RegisterHiddenField("__VIEWSTATEGUID", viewStateGuidKey);
            }
        }
    }
}

